/*
 * Copyright 2012 Finbudget.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package finbudgettable.client.ui.grid.cell.longg;

import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import finbudgettable.client.ui.grid.cell.input.InputEditableCell;

public class LongCell extends InputEditableCell<Long> {

    interface Resources extends ClientBundle {

        interface Style extends CssResource {

            @ClassName("b-long-cell__text")
            String bLongCell__text();

            @ClassName("b-long-cell")
            String bLongCell();

            @ClassName("b-long-filter-panel")
            String bLongFilterPanel();
        }

        @Source("LongCell.css")
        Style style();
    }

    static final Resources RESOURCES = GWT.create(Resources.class);

    static {
        RESOURCES.style().ensureInjected();
    }

    public LongCell(boolean nullable) {
        super(nullable);
    }

    @Override
    protected boolean validate(String value) {
        try {
            convertEditedValueToValue(value);
            return true;
        } catch (NumberFormatException ex) {
            return false;
        }
    }

    @Override
    protected String convertValueToEditedValue(Long value) {
        return value != null ? value.toString() : "";
    }

    @Override
    protected Long convertEditedValueToValue(String editedValue) {
        if (isNullable() && (editedValue == null || editedValue.isEmpty())) {
            return null;
        } else {
            return Long.valueOf(editedValue);
        }
    }

    @Override
    protected String getValueToDisplay(Long value) {
        return value != null ? value.toString() : "";
    }

    @Override
    protected String getInnerCellClassName() {
        return RESOURCES.style().bLongCell__text();
    }
}
